package com.questiono;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.FragmentActivity;
import android.util.Base64;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.model.GraphUser;
import com.facebook.widget.ProfilePictureView;
import com.sameer.questiono.R;


public class HomeScreen extends FragmentActivity {
	
	private MainFragment mainFragment;
    String TAG_ERROR="error";
    private PlayScreen play;	
    public static final String PREFS_NAME = "Questiono";
    private ConnectionDetector cd;
    public static final int music = R.raw.here;
    
    
    
	public void showAlertDialog(Context context, String title, String message, Boolean status) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		// Setting Dialog Title
		alertDialog.setTitle(title);

		// Setting Dialog Message
		alertDialog.setMessage(message);

		// Setting alert dialog icon
		alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

		// Setting OK Button
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		});

		// Showing Alert Message
		alertDialog.show();
		
	}
    
	public void onExit(View view)
	{
		moveTaskToBack(true);
	}
    
    public void onPlay(View view)
	{
    	boolean isInternetPresent;
		cd = new ConnectionDetector(getApplicationContext());
		isInternetPresent=cd.isConnectingToInternet();
    	
    	if (!isInternetPresent)
		{
			showAlertDialog(HomeScreen.this, "No Internet Connection",
					"You don't have internet connection.", false); 
			/*Intent nextScreen = new Intent(getApplicationContext(), HomeScreen.class);
			startActivity(nextScreen);*/
		}else
		{
			
			
			
		    play=new PlayScreen();
		  
	        /*play.setSession(session);*/
	        Intent nextScreen = new Intent(getApplicationContext(), play.getClass());
	        /*nextScreen.putExtra("email", email);
			nextScreen.getIntExtra("pic", profilePictureView);
			nextScreen.putExtra("email", email);*/
	    	   startActivity(nextScreen);
		}
	}

    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		/*MediaPlayer mediaPlayer = new MediaPlayer();
		mediaPlayer.create(this, R.raw.loop);
		mediaPlayer.setLooping(true);
		mediaPlayer.start();*/
		PowerManager powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
		WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
		wakeLock.acquire();
		
		PackageInfo info = null;
		try {
			info = getPackageManager().getPackageInfo("com.sameer.questiono",  PackageManager.GET_SIGNATURES);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (Signature signature : info.signatures)
		    {
		        MessageDigest md = null;
				try {
					md = MessageDigest.getInstance("SHA");
				} catch (NoSuchAlgorithmException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        md.update(signature.toByteArray());
		        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
		    }
		
		
		super.onCreate(savedInstanceState);
		if (savedInstanceState == null) {
		        // Add the fragment on initial activity setup
		        mainFragment = new MainFragment();
		        getSupportFragmentManager()
		        .beginTransaction()
		        .add(android.R.id.content, mainFragment)
		        .commit();
		    } else {
		        // Or set the fragment from restored state info
		        mainFragment = (MainFragment) getSupportFragmentManager()
		        .findFragmentById(android.R.id.content);
		    }
		  
		


		
	
		    }
	
	// Adding Override for the Hardware Back key
			@Override
			public boolean onKeyDown(int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					moveTaskToBack(true);
					return true;
				}
				return super.onKeyDown(keyCode, event);
			}
}
